import { SendgridIcon } from '@/components/icons'
import type { BlockConfig } from '@/blocks/types'
import type { SendMailResult } from '@/tools/sendgrid/types'

export const SendGridBlock: BlockConfig<SendMailResult> = {
  type: 'sendgrid',
  name: 'SendGrid',
  description: 'Send emails and manage contacts, lists, and templates with SendGrid',
  longDescription:
    'Integrate SendGrid into your workflow. Send transactional emails, manage marketing contacts and lists, and work with email templates. Supports dynamic templates, attachments, and comprehensive contact management.',
  docsLink: 'https://docs.sim.ai/tools/sendgrid',
  category: 'tools',
  bgColor: '#1A82E2',
  icon: SendgridIcon,

  subBlocks: [
    {
      id: 'operation',
      title: 'Operation',
      type: 'dropdown',
      options: [
        // Mail Operations
        { label: 'Send Mail', id: 'send_mail' },
        // Contact Operations
        { label: 'Add Contact', id: 'add_contact' },
        { label: 'Get Contact', id: 'get_contact' },
        { label: 'Search Contacts', id: 'search_contacts' },
        { label: 'Delete Contacts', id: 'delete_contacts' },
        // List Operations
        { label: 'Create List', id: 'create_list' },
        { label: 'Get List', id: 'get_list' },
        { label: 'List All Lists', id: 'list_all_lists' },
        { label: 'Delete List', id: 'delete_list' },
        { label: 'Add Contacts to List', id: 'add_contacts_to_list' },
        { label: 'Remove Contacts from List', id: 'remove_contacts_from_list' },
        // Template Operations
        { label: 'Create Template', id: 'create_template' },
        { label: 'Get Template', id: 'get_template' },
        { label: 'List Templates', id: 'list_templates' },
        { label: 'Delete Template', id: 'delete_template' },
        { label: 'Create Template Version', id: 'create_template_version' },
      ],
      value: () => 'send_mail',
    },
    {
      id: 'apiKey',
      title: 'SendGrid API Key',
      type: 'short-input',
      password: true,
      placeholder: 'Enter your SendGrid API key',
      required: true,
    },
    // Send Mail fields
    {
      id: 'from',
      title: 'From Email',
      type: 'short-input',
      placeholder: 'sender@yourdomain.com',
      condition: { field: 'operation', value: 'send_mail' },
      required: true,
    },
    {
      id: 'fromName',
      title: 'From Name',
      type: 'short-input',
      placeholder: 'Sender Name',
      condition: { field: 'operation', value: 'send_mail' },
    },
    {
      id: 'to',
      title: 'To Email',
      type: 'short-input',
      placeholder: 'recipient@example.com',
      condition: { field: 'operation', value: 'send_mail' },
      required: true,
    },
    {
      id: 'toName',
      title: 'To Name',
      type: 'short-input',
      placeholder: 'Recipient Name',
      condition: { field: 'operation', value: 'send_mail' },
    },
    {
      id: 'mailSubject',
      title: 'Subject',
      type: 'short-input',
      placeholder: 'Email subject (required unless using template)',
      condition: { field: 'operation', value: 'send_mail' },
    },
    {
      id: 'content',
      title: 'Content',
      type: 'long-input',
      placeholder: 'Email body content (required unless using template)',
      condition: { field: 'operation', value: 'send_mail' },
    },
    {
      id: 'contentType',
      title: 'Content Type',
      type: 'dropdown',
      options: [
        { label: 'Plain Text', id: 'text/plain' },
        { label: 'HTML', id: 'text/html' },
      ],
      value: () => 'text/plain',
      condition: { field: 'operation', value: 'send_mail' },
    },
    {
      id: 'cc',
      title: 'CC',
      type: 'short-input',
      placeholder: 'cc@example.com',
      condition: { field: 'operation', value: 'send_mail' },
    },
    {
      id: 'bcc',
      title: 'BCC',
      type: 'short-input',
      placeholder: 'bcc@example.com',
      condition: { field: 'operation', value: 'send_mail' },
    },
    {
      id: 'replyTo',
      title: 'Reply To',
      type: 'short-input',
      placeholder: 'replyto@example.com',
      condition: { field: 'operation', value: 'send_mail' },
    },
    {
      id: 'replyToName',
      title: 'Reply To Name',
      type: 'short-input',
      placeholder: 'Reply To Name',
      condition: { field: 'operation', value: 'send_mail' },
    },
    {
      id: 'mailTemplateId',
      title: 'Template ID',
      type: 'short-input',
      placeholder: 'SendGrid template ID',
      condition: { field: 'operation', value: 'send_mail' },
    },
    {
      id: 'dynamicTemplateData',
      title: 'Dynamic Template Data',
      type: 'code',
      placeholder: '{"name": "John", "order_id": "12345"}',
      condition: { field: 'operation', value: 'send_mail' },
    },
    // File upload (basic mode)
    {
      id: 'attachmentFiles',
      title: 'Attachments',
      type: 'file-upload',
      canonicalParamId: 'attachments',
      placeholder: 'Upload files to attach',
      condition: { field: 'operation', value: 'send_mail' },
      mode: 'basic',
      multiple: true,
      required: false,
    },
    // Variable reference (advanced mode)
    {
      id: 'attachments',
      title: 'Attachments',
      type: 'short-input',
      canonicalParamId: 'attachments',
      placeholder: 'Reference files from previous blocks',
      condition: { field: 'operation', value: 'send_mail' },
      mode: 'advanced',
      required: false,
    },
    // Contact fields
    {
      id: 'email',
      title: 'Email',
      type: 'short-input',
      placeholder: 'contact@example.com',
      condition: { field: 'operation', value: ['add_contact'] },
      required: true,
    },
    {
      id: 'firstName',
      title: 'First Name',
      type: 'short-input',
      placeholder: 'John',
      condition: { field: 'operation', value: ['add_contact'] },
    },
    {
      id: 'lastName',
      title: 'Last Name',
      type: 'short-input',
      placeholder: 'Doe',
      condition: { field: 'operation', value: ['add_contact'] },
    },
    {
      id: 'customFields',
      title: 'Custom Fields',
      type: 'code',
      placeholder: '{"custom_field_1": "value1"}',
      condition: { field: 'operation', value: ['add_contact'] },
    },
    {
      id: 'contactListIds',
      title: 'List IDs',
      type: 'short-input',
      placeholder: 'Comma-separated list IDs',
      condition: { field: 'operation', value: ['add_contact'] },
    },
    {
      id: 'contactId',
      title: 'Contact ID',
      type: 'short-input',
      placeholder: 'Contact ID',
      condition: { field: 'operation', value: ['get_contact'] },
      required: true,
    },
    {
      id: 'query',
      title: 'Search Query',
      type: 'long-input',
      placeholder: "email LIKE '%example.com%'",
      condition: { field: 'operation', value: ['search_contacts'] },
      required: true,
    },
    {
      id: 'contactIds',
      title: 'Contact IDs',
      type: 'short-input',
      placeholder: 'Comma-separated contact IDs',
      condition: {
        field: 'operation',
        value: ['delete_contacts', 'remove_contacts_from_list'],
      },
      required: true,
    },
    {
      id: 'contacts',
      title: 'Contacts (JSON Array)',
      type: 'code',
      placeholder: '[{"email": "user@example.com", "first_name": "John"}]',
      condition: { field: 'operation', value: 'add_contacts_to_list' },
      required: true,
    },
    // List fields
    {
      id: 'listName',
      title: 'List Name',
      type: 'short-input',
      placeholder: 'List name',
      condition: { field: 'operation', value: ['create_list'] },
      required: true,
    },
    {
      id: 'listId',
      title: 'List ID',
      type: 'short-input',
      placeholder: 'List ID',
      condition: {
        field: 'operation',
        value: ['get_list', 'delete_list', 'add_contacts_to_list', 'remove_contacts_from_list'],
      },
      required: true,
    },
    {
      id: 'listPageSize',
      title: 'Page Size',
      type: 'short-input',
      placeholder: '100',
      condition: { field: 'operation', value: 'list_all_lists' },
    },
    // Template fields
    {
      id: 'templateName',
      title: 'Template Name',
      type: 'short-input',
      placeholder: 'Template name',
      condition: { field: 'operation', value: ['create_template'] },
      required: true,
    },
    {
      id: 'templateId',
      title: 'Template ID',
      type: 'short-input',
      placeholder: 'Template ID',
      condition: {
        field: 'operation',
        value: ['get_template', 'delete_template', 'create_template_version'],
      },
      required: true,
    },
    {
      id: 'generation',
      title: 'Template Generation',
      type: 'dropdown',
      options: [
        { label: 'Dynamic', id: 'dynamic' },
        { label: 'Legacy', id: 'legacy' },
      ],
      value: () => 'dynamic',
      condition: { field: 'operation', value: 'create_template' },
    },
    {
      id: 'templateGenerations',
      title: 'Filter by Generation',
      type: 'short-input',
      placeholder: 'legacy, dynamic, or both',
      condition: { field: 'operation', value: 'list_templates' },
    },
    {
      id: 'templatePageSize',
      title: 'Page Size',
      type: 'short-input',
      placeholder: '20',
      condition: { field: 'operation', value: 'list_templates' },
    },
    {
      id: 'versionName',
      title: 'Version Name',
      type: 'short-input',
      placeholder: 'Version name',
      condition: { field: 'operation', value: 'create_template_version' },
      required: true,
    },
    {
      id: 'templateSubject',
      title: 'Template Subject',
      type: 'short-input',
      placeholder: 'Email subject',
      condition: { field: 'operation', value: 'create_template_version' },
      required: true,
    },
    {
      id: 'htmlContent',
      title: 'HTML Content',
      type: 'code',
      placeholder: '<html><body>{{name}}</body></html>',
      condition: { field: 'operation', value: 'create_template_version' },
    },
    {
      id: 'plainContent',
      title: 'Plain Text Content',
      type: 'long-input',
      placeholder: 'Plain text content',
      condition: { field: 'operation', value: 'create_template_version' },
    },
    {
      id: 'active',
      title: 'Active',
      type: 'dropdown',
      options: [
        { label: 'Yes', id: 'true' },
        { label: 'No', id: 'false' },
      ],
      value: () => 'true',
      condition: { field: 'operation', value: 'create_template_version' },
    },
  ],

  tools: {
    access: [
      'sendgrid_send_mail',
      'sendgrid_add_contact',
      'sendgrid_get_contact',
      'sendgrid_search_contacts',
      'sendgrid_delete_contacts',
      'sendgrid_create_list',
      'sendgrid_get_list',
      'sendgrid_list_all_lists',
      'sendgrid_delete_list',
      'sendgrid_add_contacts_to_list',
      'sendgrid_remove_contacts_from_list',
      'sendgrid_create_template',
      'sendgrid_get_template',
      'sendgrid_list_templates',
      'sendgrid_delete_template',
      'sendgrid_create_template_version',
    ],
    config: {
      tool: (params) => `sendgrid_${params.operation}`,
      params: (params) => {
        const {
          operation,
          mailSubject,
          mailTemplateId,
          listName,
          templateName,
          versionName,
          templateSubject,
          contactListIds,
          templateGenerations,
          listPageSize,
          templatePageSize,
          ...rest
        } = params

        // Map renamed fields back to tool parameter names
        return {
          ...rest,
          ...(mailSubject && { subject: mailSubject }),
          ...(mailTemplateId && { templateId: mailTemplateId }),
          ...(listName && { name: listName }),
          ...(templateName && { name: templateName }),
          ...(versionName && { name: versionName }),
          ...(templateSubject && { subject: templateSubject }),
          ...(contactListIds && { listIds: contactListIds }),
          ...(templateGenerations && { generations: templateGenerations }),
          ...(listPageSize && { pageSize: listPageSize }),
          ...(templatePageSize && { pageSize: templatePageSize }),
        }
      },
    },
  },

  inputs: {
    operation: { type: 'string', description: 'Operation to perform' },
    apiKey: { type: 'string', description: 'SendGrid API key' },
    // Mail inputs
    from: { type: 'string', description: 'Sender email address' },
    fromName: { type: 'string', description: 'Sender name' },
    to: { type: 'string', description: 'Recipient email address' },
    toName: { type: 'string', description: 'Recipient name' },
    mailSubject: { type: 'string', description: 'Email subject' },
    content: { type: 'string', description: 'Email content' },
    contentType: { type: 'string', description: 'Content type' },
    cc: { type: 'string', description: 'CC email address' },
    bcc: { type: 'string', description: 'BCC email address' },
    replyTo: { type: 'string', description: 'Reply-to email address' },
    replyToName: { type: 'string', description: 'Reply-to name' },
    mailTemplateId: { type: 'string', description: 'Template ID for sending mail' },
    dynamicTemplateData: { type: 'json', description: 'Dynamic template data' },
    attachmentFiles: { type: 'json', description: 'Files to attach (UI upload)' },
    attachments: { type: 'array', description: 'Files to attach (UserFile array)' },
    // Contact inputs
    email: { type: 'string', description: 'Contact email' },
    firstName: { type: 'string', description: 'Contact first name' },
    lastName: { type: 'string', description: 'Contact last name' },
    customFields: { type: 'json', description: 'Custom fields' },
    contactId: { type: 'string', description: 'Contact ID' },
    contactIds: { type: 'string', description: 'Comma-separated contact IDs' },
    contacts: { type: 'json', description: 'Array of contact objects' },
    query: { type: 'string', description: 'Search query' },
    contactListIds: { type: 'string', description: 'Comma-separated list IDs for contact' },
    // List inputs
    listName: { type: 'string', description: 'List name' },
    listId: { type: 'string', description: 'List ID' },
    listPageSize: { type: 'number', description: 'Page size for listing lists' },
    // Template inputs
    templateName: { type: 'string', description: 'Template name' },
    templateId: { type: 'string', description: 'Template ID' },
    generation: { type: 'string', description: 'Template generation' },
    templateGenerations: { type: 'string', description: 'Filter templates by generation' },
    templatePageSize: { type: 'number', description: 'Page size for listing templates' },
    versionName: { type: 'string', description: 'Template version name' },
    templateSubject: { type: 'string', description: 'Template subject' },
    htmlContent: { type: 'string', description: 'HTML content' },
    plainContent: { type: 'string', description: 'Plain text content' },
    active: { type: 'boolean', description: 'Whether template version is active' },
  },

  outputs: {
    success: { type: 'boolean', description: 'Operation success status' },
    messageId: { type: 'string', description: 'Email message ID (send_mail)' },
    id: { type: 'string', description: 'Resource ID' },
    jobId: { type: 'string', description: 'Job ID for async operations' },
    email: { type: 'string', description: 'Email address' },
    firstName: { type: 'string', description: 'First name' },
    lastName: { type: 'string', description: 'Last name' },
    contacts: { type: 'json', description: 'Array of contacts' },
    contactCount: { type: 'number', description: 'Number of contacts' },
    lists: { type: 'json', description: 'Array of lists' },
    templates: { type: 'json', description: 'Array of templates' },
    message: { type: 'string', description: 'Status or success message' },
    name: { type: 'string', description: 'Resource name' },
    generation: { type: 'string', description: 'Template generation' },
  },
}
